/**
 * \file valarray.hpp
 * 
 * \section COPYRIGHT
 *
 * Basis: A 3D Mathematics Library
 *
 * ---------------------------------------------------------------------
 *
 * Copyright (c) 2010, Don Olmstead
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *  3. Neither the name of organization nor the names of its contributors may be
 *     used to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef BASIS_VALARRAY_HPP_INCLUDED
#define BASIS_VALARRAY_HPP_INCLUDED

#include <basis/detail/array_storage.hpp>

namespace basis
{
	template <typename T, typename Rep = detail::array_storage<T> >
	class valarray
	{

		//------------------------------------------------------------
		// Member types
		//------------------------------------------------------------

		public:

			/// Value of type T
			typedef T value_type;
			/// Random access iterator
			typedef T* iterator;
			/// Constant random access iterator
			typedef const T* const_iterator;
			/// Reference to a type T
			typedef T& reference;
			/// Const reference to a type T
			typedef const T& const_reference;
			/// Unsigned integral type
			typedef std::size_t size_type;

		//------------------------------------------------------------
		// Construction/Destruction
		//------------------------------------------------------------

		public:

			/**
			 * Creates an instance of the valarray class.
			 *
			 * \param size The number of elements to create.
			 */
			explicit valarray(size_type size)
				: _values(size)
			{ }

			/**
			 * Initializes an instance of the valarray class.
			 *
			 * \param copy The internal storage representation to copy.
			 */
			valarray(const Rep& copy)
				: _values(copy)
			{ }

			/**
			 * Assignment operator for the valarray class.
			 *
			 * \param copy The valarray to copy.
			 */
			inline valarray& operator= (const valarray& copy)
			{
				// assert

				size_type array_size = size();

				for (size_type i = 0; i < array_size; ++i)
					_values[i] = copy._values[i];

				return *this;
			}

			/**
			 * Assignment operator for the valarray class.
			 *
			 * \tparam T2 An alternate data type.
			 * \tparam Rep2 An alternate data representation.
			 *
			 * \param copy The valarray to copy.
			 */
			template <typename T2, typename Rep2>
			inline valarray& operator= (const valarray<T2, Rep2>& copy)
			{
				// assert

				size_type array_size = size();

				for (size_type i = 0; i < array_size; ++i)
					_values[i] = copy[i];

				return *this;
			}

		//------------------------------------------------------------
		// Iterators
		//------------------------------------------------------------

		public:

			/**
			 * Retrieves an iterator pointing to the beginning of the valarray.
			 *
			 * \returns An iterator pointing to the beginning of the valarray.
			 */
			iterator begin()
			{
				return _values;
			}

			/**
			 * Retrieves an iterator pointing to the beginning of the valarray.
			 *
			 * \returns An iterator pointing to the beginning of the valarray.
			 */
			const_iterator begin() const
			{
				return _values;
			}

			/**
			 * Retrieves an iterator pointing to the end of the valarray.
			 *
			 * \returns An iterator pointing to the end of the valarray.
			 */
			iterator end()
			{
				return _values + _size;;
			}

			/**
			 * Retrieves an iterator pointing to the end of the valarray.
			 *
			 * \returns An iterator pointing to the end of the valarray.
			 */
			const_iterator end() const
			{
				return _values + _size;
			}

		//------------------------------------------------------------
		// Capacity
		//------------------------------------------------------------

		public:

			/**
			 * Retrieves the size of the valarray.
			 *
			 * \returns The size of the valarray.
			 */
			inline size_type size() const
			{
				return _values.size();
			}

			/**
			 * Retrieves the capacity of the valarray.
			 *
			 * \returns The capacity of the valarray.
			 */
			inline size_type capacity() const
			{
				return _values.size();
			}

		//------------------------------------------------------------
		// Element access
		//------------------------------------------------------------

		public:

			/**
			 * Array operator for the valarray class.
			 *
			 * \param i The index of the element.
			 * \returns The value of the element at the index.
			 */
			inline reference operator[] (size_type i)
			{
				return _values[i];
			}

			/**
			 * Array operator for the valarray class.
			 *
			 * \param i The index of the element.
			 * \returns The value of the element at the index.
			 */
			inline value_type operator[] (size_type i) const
			{
				return _values[i];
			}

			/**
			 * Retrieves the first element of the valarray.
			 *
			 * \returns The first element of the valarray.
			 */
			inline reference front()
			{
				return _values[0];
			}

			/**
			 * Retrieves the first element of the valarray.
			 *
			 * \returns The first element of the valarray.
			 */
			inline const_reference front() const
			{
				return _values[0];
			}

			/**
			 * Retrieves the last element of the valarray.
			 *
			 * \returns The last element of the valarray.
			 */
			inline reference back()
			{
				return _values[_size - 1];
			}

			/**
			 * Retrieves the last element of the valarray.
			 *
			 * \returns The last element of the valarray.
			 */
			inline const_reference back() const
			{
				return _values[_size - 1];
			}

		//------------------------------------------------------------
		// C-style array
		//------------------------------------------------------------

		public:

			/**
			 * Retrieves a C-style array.
			 *
			 * Useful for passing the valarray to C-APIs.
			 *
			 * \returns A C-style array.
			 */
			inline T* c_array()
			{
				return _values;
			}

			/**
			 * Retrieves a C-style array.
			 *
			 * Useful for passing the valarray to C-APIs.
			 *
			 * \returns A C-style array.
			 */
			inline const T* c_array() const
			{
				return _values;
			}

		//------------------------------------------------------------
		// Internal representation
		//------------------------------------------------------------

		public:

			/**
			 * Retrieves the internal representation of the valarray.
			 *
			 * \returns The internal representation of the valarray.
			 */
			inline Rep& rep()
			{
				return _values;
			}

			/**
			 * Retrieves the internal representation of the valarray.
			 *
			 * \returns The internal representation of the valarray.
			 */
			inline const Rep& rep() const
			{
				return _values;
			}

		private:

			/// The underlying storage of the array
			Rep _values;

	} ; // end class valarray

} // end namespace basis

#endif // end BASIS_VALARRAY_HPP_INCLUDED
